<script type="text/javascript" language="javascript" src="<?= base_url() ?>public/js/jquery.validate.js"></script>
<script src="<?php echo base_url(); ?>public/js/chosen.jquery.min.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>public/js/ajax-chosen.min.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>public/js/validation-custom-text.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>public/js/jquery.jqprint-0.3.js" type="text/javascript"></script>
<script type="text/javascript">
    var isIssueAdd = true;
    
    $(document).ready(function(){
        changeIssueForm();
        clearIssueDetail();        
        $(".iframe").colorbox({
            iframe:true, 
            width:"90%", 
            height:"90%"
        });
                
        $("#issue-official-order").change(function(){
            $.ajax({
                url: "<?php echo base_url(); ?>index.php/admin_stock/getMedalOrder",
                data : "issue_id="+$(this).val(),
                type: "POST",
                dataType: "json",
                success: function(result){                           
                    clearIssueDetail();
                    $.each(result, function(key, val) {
                        $("#issue-detail-panel td input[c='"+ val.Medal_Class_ID +"'][t='"+val.Medal_Type_ID+"']").val(Number(val.possible_amount));
                    });
                }               
            });
        }).chosen();
        $("#sissuenumber").keyup(function(){
            oTable.fnReloadAjax( "<?php echo base_url(); ?>index.php/admin_stock/issue_list?"  + $("#search-issue-form").serialize());
        });
        $("#issue-detail-panel input").autoNumeric({
            aNum: "0123456789",
            aSep: "",
            dGroup: "3",
            aDec: ".",
            altDec: null,
            aSign: "",
            pSign: "p",
            vMax: "999999999999999999999999999999",
            vMin: "0",
            mDec: null,
            mRound: "S",
            aPad: false,
            aForm: false
        });
        var oTable = $('#stock-issue-list').dataTable( {
            "bSortClasses": false,
            "bPaginate": true,
            "bLengthChange": true,
            "bFilter": false,
            "bSort": true,
            "bInfo": false,
            "bAutoWidth": false,
            "aoColumns" : [
                { sWidth: '30px' },
                { sWidth: '300px' },
                { sWidth: '100px' },
                { sWidth: '20px' }
            ] ,
            "aoColumnDefs": [ 
                { "bSortable": false, "aTargets": [ 0,3] }
            ],
            "aaSorting": [[ 2, "desc" ]],
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "<?php echo base_url(); ?>index.php/admin_stock/issue_list?"  + $("#search-issue-form").serialize(),
            "fnDrawCallback": function() {
                clearIssue();               
            }
        });
        
        $("#fromissuedate").datepicker({
            onSelect: function(selected) {
                $( "#toissuedate" ).datepicker("option", "minDate", selected);                
                oTable.fnReloadAjax( "<?php echo base_url(); ?>index.php/admin_stock/issue_list?"  + $("#search-issue-form").serialize());
            }
        });
        $("#toissuedate").datepicker({
            onSelect: function(selected) {
                $("#fromissuedate").datepicker("option", "maxDate", selected);
                oTable.fnReloadAjax( "<?php echo base_url(); ?>index.php/admin_stock/issue_list?"  + $("#search-issue-form").serialize());
            }
        });
        $("#issue-form").ajaxForm({        
            beforeSerialize: function(){     
                $("#medal-data").html("");
                $("#issue-detail-panel input").each(function(){
                    if($(this).val()>0){
                        $("#medal-data").append("<input type='hidden' name='"+$(this).attr("name")+"' value='"+$(this).val()+"' />");                        
                    }
                });
            },
            beforeSubmit: function(){
                validateDefaultText('#issue-form input');
                var validate = $('#issue-form').validate().form();
                if (validate == false){
                    returnDefaultText('#issue-form input');
                    validate.error();
                    return validate; 
                }
                // waiting
                $('#form-stock-issue .loading').show();
                $('#form-stock-issue .add-button').addClass('disable-add-button');
                $('#form-stock-issue .update-button').addClass('disable-update-button');
                $('#form-stock-issue .add-button, #form-stock-issue .update-button').attr('disabled', 'disabled');
            },
            url:  '<?php echo base_url(); ?>index.php/admin_stock/stock_issue',
            dataType: 'json',
            error: function(){
                // stop waiting display error
                $('#form-stock-issue .err-msg').html("ទិន្នន័យមិនអាចរក្សាទុកបានដោយបញ្ហាបច្ចេកទេស");                
                $('#form-stock-issue .loading').hide();
                $('#form-stock-issue .add-button').removeClass('disable-add-button');
                $('#form-stock-issue .update-button').removeClass('disable-update-button');
                $('#form-stock-issue .add-button, #form-stock-issue .update-button').removeAttr('disabled');
            },
            success: function (data){                
                if (!data.result){
                    $('#form-stock-issue .err-msg').html(data.message);
                }else{            
                    $('#form-stock-issue .err-msg').html(data.message);
                    oTable.fnDraw(false);
                }
                $('#form-stock-issue .loading').hide();
                $('#form-stock-issue .add-button').removeClass('disable-add-button');
                $('#form-stock-issue .update-button').removeClass('disable-update-button');
                $('#form-stock-issue .add-button, #form-stock-issue .update-button').removeAttr('disabled');
            }
        });
        
        $("#issue-cancel").click(function(event) {
            event.preventDefault();
            clearIssue();
            changeIssueForm();
        });
        
        $(".edit-issue").live('click', function(event){
            event.preventDefault();
            $("#issueid").val($(this).attr("rel"));
            isIssueAdd = false;
            changeIssueForm();
            readIssue();            
        });
        
        $("#stock-issue-list tbody").click(function(event) {
            $(oTable.fnSettings().aoData).each(function (){
                $(this.nTr).removeClass('row_selected');
            });
            clearIssue();
            $("#issue-detail-panel td input").hide();
            $(event.target.parentNode).addClass('row_selected');
            var id = $(event.target.parentNode).find(".edit-issue").attr("rel");            
            $.ajax({
                url: "<?php echo base_url(); ?>index.php/admin_stock/read_issue",
                data : "id="+id,
                type: "POST",
                dataType: "json",
                success: function(result){
                    $("#issue-detail-panel td span").show();
                    $("#lissue_for").html(result.issue_for);
                    $("#lorganization").html(result.organization);
                    $("#lissue_number").html("");
                      
                    $.each(result.official_order_id, function(key, val) {
                        $("#lissue_number").append(val.value+" &nbsp;&nbsp;&nbsp;");
                    });
                    
                    $.ajax({
                        url: "<?php echo base_url(); ?>index.php/admin_stock/read_official",
                        data : "issue_id="+result.ID,
                        type: "POST",
                        dataType: "json",
                        success: function(result){               
                            $("#issue-detail-panel #btnPrint").show();             
                            $("#issue-detail-panel #btnPrint").attr("href", "<?= site_url('/admin_stock/printPreview/') ?>/"+id);             
                            $("#issue-detail-panel td span").html(0);
                            $.each(result, function(key, val) {
                                $("#issue-detail-panel td span[class='"+ val.Medal_Class_ID +"'][type='"+val.Medal_Type_ID+"']").html(Number(val.amount));
                            });
                        }               
                    });
                }               
            });
        });
        
    });	
        
    function changeIssueForm(){
        if(isIssueAdd){
            $("#issue-update, #issue-cancel").hide();
            $("#issue-submit").show();
        }else{
            $("#issue-update, #issue-cancel").show();
            $("#issue-submit").hide();            
        }
    }
    
    function clearIssue(){    
        isIssueAdd = true;
        clearIssueDetail();
        changeIssueForm();
        $("#issue-detail-panel td input").show();
        $("#form-stock-issue .err-msg, #issue-detail-panel td span, #issue-detail-panel #block-content b span").html("");  
        $.post("<?php echo base_url(); ?>index.php/admin_stock/getAvailableOfficerOrder", "", function(callback){  
            $("#issue-form #issue-official-order").html(callback).removeAttr("disabled").trigger("liszt:updated");  
        });
                
        $("#issue-form #issuenumber").val("លេខ​​​​");  
        $("#issue-form #issue-date").val("កាលបរិច្ឆេទ");  
        $("#issue-form #forperson").val("បញ្ចេញជូន...");  
        $("#issue-form #organization").val("អង្គភាព...");  
        $("#issue-form #issueid").val("");  
        $('#issue-form .populated-text').each(function () {
            populateText($(this));
        }); 
    }
    
    function readIssue(){
        var id = $("#issueid").val(); 
        $.ajax({
            url: "<?php echo base_url(); ?>index.php/admin_stock/read_issue",
            data : "id="+id,
            type: "POST",
            dataType: "json",
            success: function(result){
                $("#issue-form #issuenumber").val(result.issue_number);
                $("#issue-form #issue-official-order").html("");
                $.each(result.official_order_id, function(key, val) {
                    $("#issue-form #issue-official-order").append("<option value='"+val.id+"' selected='selected'>"+val.value+"</option>");
                });
                $("#issue-form #issue-official-order") .attr("disabled", "disabled").trigger("liszt:updated");  
                $("#issue-form #issue-date").datepicker("setDate", new Date(result.date));
                $("#issue-form #forperson").val(result.issue_for);
                $("#issue-form #organization").val(result.organization);
                setDataEntered($("#issue-form")); 
            }               
        });
    }

    function clearIssueDetail(){
        $("#issue-detail-panel td span, #issue-detail-panel #btnPrint").hide();
        $("#issue-detail-panel #lissue_for, #issue-detail-panel #lorganization, #issue-detail-panel #lissue_number").html("");
        $("#issue-detail-panel td input").show();
        $('#issue-detail-panel input').each(function () {
            $(this).val(0);
            populateText($(this));
        }); 
    }
</script>
<div id="left-content">
    <div id="medal-issue-panel" class="content-block">
        <div class="block-header">ប័ណ្ណបញ្ចេញឃ្លាំងថ្មី</div>
        <div id="block-content">
            <div id="search-panel">
                <fieldset>
                    <legend>ស្វែងរក</legend>
                    <form id="search-issue-form" method="get" autocomplete="off" name="search-official-form">
                        <input type="text" name="sissuenumber" id="sissuenumber" value="លេខ​" class="text input required populated-text" />
                        <input type="text" name="fromissuedate" id="fromissuedate" value="ពីថ្ងៃទី​" class="input text datepicker populated-text" />
                        <input type="text" name="toissuedate" id="toissuedate" value="ដល់ថ្ងៃទី​"  class="input text datepicker populated-text" />
                    </form>
                </fieldset>
            </div>
            <div id="issue-list">
                <table id="stock-issue-list"  class="no-vertical-border">
                    <thead>
                        <tr>
                            <th class="index-col">ល.រ</th>
                            <th class="head">លេខប័ណ្ណ​</th>
                            <th class="head">កាលបរិច្ឆេទ</th>
                            <th align="center" class="blank-header" ></th>
                        </tr>
                    </thead>
                </table>
                <div class="break"></div>
            </div>
            <div class="form-add-panel" id="form-stock-issue">
                <div id="title">បញ្ជូលក្រឹត្យបន្ថែម៖</div>
                <div class="err-msg"></div>
                <div class="loading"><img src="<?php echo base_url(); ?>public/images/forms/loading-bar.gif" /></div>
                <form id="issue-form" method="post" autocomplete="off" name="issue-form">
                    <input type="text" name="issuenumber" id="issuenumber" value="លេខ" class="text input required left populated-text" />
                    <input type="text" name="issue-date" id="issue-date" value="កាលបរិច្ឆេទ"  class="input text datepicker required left populated-text dateDefault" />
                    <select id="issue-official-order" data-placeholder="ព្រះរាជាក្រឹត្យ" multiple="multiple" name="issue-official-order[]" class="required"/>
                    <?php
                    foreach ($official_orders as $official_order):
                        echo "<option value='{$official_order['ID']}'>{$official_order['Order_Number']}</option>";
                    endforeach;
                    ?>
                    </select>
                    <div id="medal-data"></div>
                    <input type="text" name="forperson" id="forperson" value="បញ្ចេញជូន..." class="text input required populated-text" />
                    <input type="text" name="organization" id="organization" value="អង្គភាព..." class="text input required populated-text" />
                    <input type="hidden" name="issueid" id="issueid" />
                    <input type="submit" id="issue-cancel" name="issue-cancel" value="" class="cancel-button right" />
                    <input type="submit" id="issue-update" name="issue-update" value="" class="update-button right" />
                    <input type="submit" id="issue-submit" value="" class="add-button right" />
                </form>
                <div class="break"></div>
            </div>
        </div>
    </div>
    <div id="issue-detail-panel" class="content-block">
        <form id="form-issue-detail" autocomplete="off" >
            <div class="block-header">ប័ណ្ណបញ្ចេញឃ្លាំងលំអិត
                <div class="right"><a id="btnPrint" class='iframe' >បោះពុម្ភ</a></div>
            </div>
            <div id="block-content">
                <!-- ប័ណ្ណបញ្ចេញឃ្លាំងលំអិត information  -->
                <div>- បញ្ចេញជូន៖ <b><span id="lissue_for"> </span></b></div>
                <div>- អង្គភាព៖ <b><span id="lorganization"> </span></b></div>
                <div>- យោង៖ <b><span id="lissue_number"> </span></b></div>
                <div>
                    <table id="data-table" class="no-vertical-border medal-detail">
                        <tr>
                            <th scope="col">ប្រភេទ</th>
                            <th scope="col">មហាសិរីវឌ្ឍន៌</th>
                            <th scope="col">មហាសេនា</th>
                            <th scope="col">ធិបឌិន្ទុ</th>
                            <th scope="col">សេនា</th>
                            <th scope="col">អស្សរិទ្ធិ</th>
                        </tr>
                        <tr>
                            <th scope="row">ព្រះរាជាណាចក្រកម្ពុជា</th>
                            <td><span class="1" type="1"></span><input type="text" name="medal[1][1]" c="1" t="1" value="0"  /></td>
                            <td><span class="2" type="1"></span><input type="text" name="medal[1][2]"  c="2" t="1" value="0"  /></td>
                            <td><span class="3" type="1"></span><input type="text" name="medal[1][3]"  c="3" t="1" value="0"  /></td>
                            <td><span class="4" type="1"></span><input type="text" name="medal[1][4]"  c="4" t="1" value="0"  /></td>
                            <td><span class="5" type="1"></span><input type="text" name="medal[1][5]"  c="5" t="1" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">សម្តេចព្រះមហាក្សត្រីយានី</th>
                            <td><span class="1" type="2"></span><input type="text" name="medal[2][1]"  c="1" t="2" value="0"  /></td>
                            <td><span class="2" type="2"></span><input type="text" name="medal[2][2]"  c="2" t="2" value="0"  /></td>
                            <td><span class="3" type="2"></span><input type="text" name="medal[2][3]"  c="3" t="2" value="0"  /></td>
                            <td><span class="4" type="2"></span><input type="text" name="medal[2][4]"  c="4" t="2" value="0"  /></td>
                            <td><span class="5" type="2"></span><input type="text" name="medal[2][5]"  c="5" t="2" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">សុវត្ថារា</th>
                            <td><span class="1" type="3"></span><input type="text" name="medal[3][1]"  c="1" t="3" value="0"  /></td>
                            <td><span class="2" type="3"></span><input type="text" name="medal[3][2]"  c="2" t="3" value="0"  /></td>
                            <td><span class="3" type="3"></span><input type="text" name="medal[3][3]"  c="3" t="3" value="0"  /></td>
                            <td><span class="4" type="3"></span><input type="text" name="medal[3][4]"  c="4" t="3" value="0"  /></td>
                            <td><span class="5" type="3"></span><input type="text" name="medal[3][5]"  c="5" t="3" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">មុនីសារ៉ាភ័ណ្ឌ</th>
                            <td><span class="1" type="4"></span><input type="text" name="medal[4][1]"  c="1" t="4" value="0"  /></td>
                            <td><span class="2" type="4"></span><input type="text" name="medal[4][2]"  c="2" t="4" value="0"  /></td>
                            <td><span class="3" type="4"></span><input type="text" name="medal[4][3]"  c="3" t="4" value="0"  /></td>
                            <td><span class="4" type="4"></span><input type="text" name="medal[4][4]"  c="4" t="4" value="0"  /></td>
                            <td><span class="5" type="4"></span><input type="text" name="medal[4][5]"  c="5" t="4" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">សហមេត្រី</th>
                            <td><span class="1" type="5"></span><input type="text" name="medal[5][1]"  c="1" t="5" value="0"  /></td>
                            <td><span class="2" type="5"></span><input type="text" name="medal[5][2]"  c="2" t="5" value="0"  /></td>
                            <td><span class="3" type="5"></span><input type="text" name="medal[5][3]"  c="3" t="5" value="0"  /></td>
                            <td><span class="4" type="5"></span><input type="text" name="medal[5][4]"  c="4" t="5" value="0"  /></td>
                            <td><span class="5" type="5"></span><input type="text" name="medal[5][5]"  c="5" t="5" value="0"  /></td>
                        </tr>
                    </table>
                </div>
                <div>
                    <table id="data-table" class="no-vertical-border">
                        <tr>
                            <th scope="col">ប្រភេទ</th>
                            <th scope="col">មាស</th>
                            <th scope="col">ប្រាក់</th>
                            <th scope="col">សំរឹទ្ធ</th>
                        </tr>
                        <tr>
                            <th scope="row">ការពារជាតិ</th>
                            <td><span class="6" type="6"></span><input type="text" name="medal[6][6]"  c="6" t="6" value="0"  /></td>
                            <td><span class="7" type="6"></span><input type="text" name="medal[6][7]"  c="7" t="6" value="0"  /></td>
                            <td><span class="8" type="6"></span><input type="text" name="medal[6][8]"  c="8" t="6" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">ការងារ</th>
                            <td><span class="6" type="7"></span><input type="text" name="medal[7][6]"  c="6" t="7" value="0"  /></td>
                            <td><span class="7" type="7"></span><input type="text" name="medal[7][7]"  c="7" t="7" value="0"  /></td>
                            <td><span class="8" type="7"></span><input type="text" name="medal[7][8]"  c="8" t="7" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">ស្ថាបនាជាតិ</th>
                            <td><span class="6" type="8"></span><input type="text" name="medal[8][6]"  c="6" t="8" value="0"  /></td>
                            <td><span class="7" type="8"></span><input type="text" name="medal[8][7]"  c="7" t="8" value="0"  /></td>
                            <td><span class="8" type="8"></span><input type="text" name="medal[8][8]"  c="8" t="8" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">ស្ត្រីវឌ្ឍនា</th>
                            <td><span class="6" type="9"></span><input type="text" name="medal[9][6]"  c="6" t="9" value="0"  /></td>
                            <td><span class="7" type="9"></span><input type="text" name="medal[9][7]"  c="7" t="9" value="0"  /></td>
                            <td><span class="8" type="9"></span><input type="text" name="medal[9][8]"  c="8" t="9" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">ខេមរាកីឡារិទ្ធិ</th>
                            <td><span class="6" type="10"></span><input type="text" name="medal[10][6]"  c="6" t="10" value="0"  /></td>
                            <td><span class="7" type="10"></span><input type="text" name="medal[10][7]"  c="7" t="10" value="0"  /></td>
                            <td><span class="8" type="10"></span><input type="text" name="medal[10][8]"  c="8" t="10" value="0"  /></td>
                        </tr>
                    </table>
                </div>
                <div>
                    <table id="data-table" class="no-vertical-border">
                        <tr>
                            <th scope="col">ប្រភេទ</th>
                            <th scope="col">&nbsp;</th>
                        </tr>
                        <tr>
                            <th scope="row">ជាតូបការ</th>
                            <td><span class="null" type="11"></span><input type="text" name="medal[11]"  c="null" t="11" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">ឯករាជ្យជាតិ</th>
                            <td><span class="null" type="12"></span><input type="text" name="medal[12]"  c="null" t="12" value="0"  /></td>
                        </tr>
                        <tr>
                            <th scope="row">សេនាជ័យសិទ្ធ</th>
                            <td><span class="null" type="13"></span><input type="text" name="medal[13]"  c="null" t="13"  value="0"  /></td>
                        </tr>
                    </table>
                </div>
            </div>
        </form>
    </div>
</div>
<!--<div id="related-activity" class="content-block right">
        <div class="block-header">ប្រតិបត្តិសំខាន់ៗ</div>
    <div id="block-content"></div>
</div>-->